﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace HYRElectric.Dimension
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdWireAmountDimensions : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			Transaction transaction = null;
			using (DlgWireAmountDimensions dlgWireAmountDimensions = new DlgWireAmountDimensions(cmdData.Application))
			{
				dlgWireAmountDimensions.DialogResult = dlgWireAmountDimensions.ShowDialog();
				DialogResult dialogResult = dlgWireAmountDimensions.DialogResult;
				if (dialogResult != DialogResult.OK)
				{
					if (dialogResult != DialogResult.Cancel)
					{
						goto IL_17B;
					}
				}
				else
				{
					try
					{
						for (;;)
						{
							FamilySymbol symbol = CmdWireAmountDimensions.GetSymbol(activeUIDocument, dlgWireAmountDimensions);
							transaction = new Transaction(activeUIDocument.ActiveView.Document, "HYWD");
							transaction.Start();
                            Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new ElectricDim.WireFilter(), "请选择要标注根数的导线：");
							XYZ globalPoint = reference.GlobalPoint;
							Wire wire = activeUIDocument.Document.GetElement(reference) as Wire;
							wire.get_Parameter(BuiltInParameter.RBS_ELEC_WIRE_TICKMARK_STATE).Set(2);
							this.IsContainTag(activeUIDocument, wire.Id);
							int num = Convert.ToInt32(dlgWireAmountDimensions.txtWireNum.Text.Trim());
							wire.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).Set(num.ToString());
							wire.get_Parameter(BuiltInParameter.RBS_ELEC_WIRE_HOT_ADJUSTMENT).Set(num - 1);
							wire.get_Parameter(BuiltInParameter.RBS_ELEC_WIRE_GROUND_ADJUSTMENT).Set(1);
							wire.get_Parameter(BuiltInParameter.RBS_ELEC_WIRE_NEUTRAL_ADJUSTMENT).Set(1);
							this.CreateIndependentTag(activeUIDocument.ActiveView.Document, wire, globalPoint).ChangeTypeId(symbol.Id);
							transaction.Commit();
						}
					}
					catch (Exception)
					{
						transaction.Commit();
						goto IL_17B;
					}
				}
				dlgWireAmountDimensions.Close();
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			IL_17B:
			return base.YJKExcute(cmdData, ref message, elements);
		}

		private static FamilySymbol GetSymbol(UIDocument uiDoc, DlgWireAmountDimensions dlgDime)
		{
			Family family = dlgDime.DbTag.Load(uiDoc.ActiveView.Document);
			FamilySymbol result = null;
			using (List<FamilySymbol>.Enumerator enumerator = family.GetSymbols().GetEnumerator())
			{
				if (enumerator.MoveNext())
				{
					result = enumerator.Current;
				}
			}
			return result;
		}

		private IndependentTag CreateIndependentTag(Document document, Wire wire, XYZ point)
		{
            Autodesk.Revit.DB.View activeView = document.ActiveView;
			TagMode tagMode = 0;
			TagOrientation tagOrientation = 0;
			LocationCurve locationCurve = wire.Location as LocationCurve;
			locationCurve.Curve.GetEndPoint(0);
			locationCurve.Curve.GetEndPoint(1);
			IndependentTag independentTag = document.Create.NewTag(activeView, wire, true, tagMode, tagOrientation, point);
			independentTag.TagHeadPosition = point;
			if (independentTag == null)
			{
				throw new Exception("创建标签失败！");
			}
			independentTag.get_Parameter(BuiltInParameter.LEADER_LINE).Set(0);
			return independentTag;
		}

		private void IsContainTag(UIDocument uiDoc, ElementId wireId)
		{
			foreach (Element element in new FilteredElementCollector(uiDoc.ActiveView.Document, uiDoc.ActiveView.Document.ActiveView.Id).OfClass(typeof(IndependentTag)).OfCategory(BuiltInCategory.OST_WireTags).ToElements())
			{
				IndependentTag independentTag = element as IndependentTag;
				if (independentTag.TaggedLocalElementId == wireId && (uiDoc.Document.GetElementById(independentTag.GetTypeId()) as FamilySymbol).GetParameter("标记类别").AsString() == "导线根数标记")
				{
					uiDoc.ActiveView.Document.Delete(independentTag.Id);
					uiDoc.ActiveView.Document.Regenerate();
					break;
				}
			}
		}
	}
}
